package com.crl.map.unit.ele
{
	import com.crl.core.IDisposable;
	import com.crl.factory.pool.IPool;
	import com.crl.factory.pool.IRecycleable;
	import com.crl.map.core.MapLoaderManager;
	import com.crl.map.unit.def.AbstractDef;
	import com.crl.map.unit.view.IEleView;
	import com.crl.module.cmpRpc.behavior.RemoteToken;
	
	import flash.display.Graphics;
	
	public class AbstractEle implements IEle
	{
		/**
		 * 文件路径; 
		 */		
		public var path:String;
		/**
		 * 状态; 
		 */		
		protected var _status:int=0;
		
		/**
		 * 类型; 
		 */		
		protected var _type:int;
		
		/**
		 * 关注此资源的视图列表; 
		 */		
		protected var eleViews:Vector.<IEleView>;
		
		public function AbstractEle()
		{
			eleViews=new Vector.<IEleView>();
		}
		
		public function get type():int{
			return _type;
		}
		
		public function create(def:AbstractDef):void{			
			if(_status<1){
				load(path);
			}
		}
		
		public function load(path:String):void{
			_status=1;
			var token:RemoteToken=getToken(path);
			if(token)bindTokenEvent(token,true);
		}
		
		public function getToken(path:String):RemoteToken{
			return null;
		}
		
		
		private function bindTokenEvent(token:RemoteToken,bool:Boolean):void{
			if(bool){
				token.addEventListener(RemoteEvent.SUCCESED,successHandler);
				token.addEventListener(RemoteEvent.FAILED,failedHandler);
			}else{
				token.removeEventListener(RemoteEvent.SUCCESED,successHandler);
				token.removeEventListener(RemoteEvent.FAILED,failedHandler);
			}
		}
		
		private function successHandler(event:RemoteEvent):void{
			bindTokenEvent(event.target as RemoteToken,false);
			
			bmd=event.data;
			
			_status=2;
			for each(var view:IEleView in eleViews){
				view.invalidate();
			}
		}
		
		private function failedHandler(event:RemoteEvent):void{
			_status=-1;
			bindTokenEvent(event.target as RemoteToken,false);
		}
		
		public function render(g:Graphics,def:AbstractDef):void{
			
		}
	}
}